package saleMarket.service;

import saleMarket.dao.AdministratorDao;
import saleMarket.dao.UserDao;
import saleMarket.domain.Administrator;
import saleMarket.domain.User;
import saleMarket.domain.authority.Actor;
import saleMarket.service.authority.ActorService;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

/**
 * @author 徐元坤
 * @version 1.0  12-16-2020
 */

public class AdministratorService {
    private static AdministratorService administratorService = new AdministratorService();
    private AdministratorDao administratorDao = AdministratorDao.getInstance();
    private AdministratorService(){}

    public static AdministratorService getInstance(){return administratorService;}
    public Administrator find(Integer id)throws SQLException {
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        Administrator administrator = null;
        try {
            administrator = administratorDao.find(id,conn);
            conn.commit();
        }catch (SQLException e){
            conn.rollback();
        }finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return administrator;
    }

    public Collection<Administrator> findAll()throws SQLException{
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        Collection<Administrator> administrators = null;
        try {
            administrators = administratorDao.findAll(conn);
            conn.commit();
        }catch (SQLException e){
            conn.rollback();
        }finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return administrators;
    }

    public boolean add(Administrator administrator)throws SQLException{
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        boolean added = false;
        try {
            Actor actor = ActorService.getInstance().find(3);
            //创建用户对象，密码和用户名取自student字段值no
            User user = new User(null,administrator.getNo(), administrator.getNo(), actor, null);
            Integer id = UserDao.getInstance().add(user,conn);
            administrator.setId(id);
            added = administratorDao.add(administrator,conn);
            conn.commit();
        }catch (SQLException e){
            conn.rollback();
        }finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return added;
    }

    public boolean update(Administrator administrator)throws SQLException{
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        boolean update = false;
        try {
            update = administratorDao.update(administrator,conn);
            conn.commit();
        }catch (SQLException e){
            conn.rollback();
            System.out.println(e);
        }finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return update;
    }

    public boolean delete(Integer id)throws SQLException{
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        boolean deleted = false;
        try {
             deleted =administratorDao.delete(id,conn);
            UserService.getInstance().delete(id,conn);
            conn.commit();
        }catch (SQLException e){
            conn.rollback();
        }finally {
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return deleted;
    }
}
